package com.atguigu.edu2021.mock.stage;

import com.atguigu.edu2021.mock.bean.CartInfo;
import com.atguigu.edu2021.mock.bean.CourseInfo;
import com.atguigu.edu2021.mock.bean.OrderDetail;
import com.atguigu.edu2021.mock.bean.OrderInfo;
import com.atguigu.edu2021.mock.config.AppConfig;
import com.atguigu.edu2021.mock.config.EduConstant;
import com.atguigu.edu2021.mock.factory.UserSession;
import com.atguigu.edu2021.mock.log.AppCommon;
import com.atguigu.edu2021.mock.log.AppDisplay;
import com.atguigu.edu2021.mock.log.AppMain;
import com.atguigu.edu2021.mock.log.AppPage;
import com.atguigu.edu2021.mock.log.enums.ItemType;
import com.atguigu.edu2021.mock.log.enums.PageId;
import com.atguigu.edu2021.mock.service.BaseProvinceService;
import com.atguigu.edu2021.mock.service.CartInfoService;
import com.atguigu.edu2021.mock.service.OrderDetailService;
import com.atguigu.edu2021.mock.service.OrderInfoService;
import com.atguigu.mock.util.LogUtil;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumString;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component("order")
public class OrderStage implements Stage {

    @Autowired
    BaseProvinceService baseProvinceService;

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    CartInfoService cartInfoService;


    @Override
    public boolean handle() {


        if ((UserSession.getLastPageId() == PageId.course_detail && UserSession.getBuyingCourseInfo() != null)
                || (UserSession.getCartList() != null && UserSession.getCartList().size() > 0)) {

            Integer durTime = RandomNum.getRandInt(5000, AppConfig.page_during_max_ms);

            UserSession.addTimeByDuringTime(durTime);
            //生成订单
            OrderInfo orderInfo = genOrder();

            //日志部分
            AppMain appMain = mainlog(orderInfo,durTime);
            LogUtil.logToJson(appMain);


            UserSession.setOrderInfo(orderInfo);
            UserSession.setLastPageId(PageId.order);
            return true;
        } else {
            System.out.println("该用户:" + UserSession.getUserInfo().getId() + "没有可下单的课程!");
            return false;
        }
    }


    private AppMain mainlog(OrderInfo orderInfo,Integer durTime) {
        AppCommon appCommon = UserSession.getAppCommon();

        Long orderId = orderInfo.getId();



        AppPage appPage = AppPage.builder()
                .last_page_id(UserSession.getLastPageId())
                .item(orderId.toString())
                .item_type(ItemType.order_id)
                .during_time(durTime)
                .page_id(PageId.order).build();

        List<AppDisplay> appDisplayList = AppDisplay.builder().buildList();

        AppMain appMain = AppMain.builder().common(appCommon).page(appPage).displays(appDisplayList).ts(UserSession.getCurDateTime().getTime()).checkError().build();
        return appMain;

    }


    private OrderInfo genOrder() {

        List<CartInfo> cartList = UserSession.getCartList();

        List<OrderDetail> orderDetailList = new ArrayList<>();


        //判断直接购买还是从购物车结算
        if (UserSession.getLastPageId() == PageId.course_detail && UserSession.getBuyingCourseInfo() != null) {
            CourseInfo courseInfo = UserSession.getBuyingCourseInfo();
            orderDetailList.add(genOrderDetailFromCourse(courseInfo));
        } else if (UserSession.getLastPageId() == PageId.cart && cartList.size() > 0) {
            boolean ifRepeat=false;
            for (CartInfo cartInfo : cartList) {
                for (OrderDetail orderDetail : orderDetailList) {
                    if(orderDetail.getCourseId()==cartInfo.getCourseId()){
                        ifRepeat=true;
                    }
                }

                if (ifRepeat) continue;
                orderDetailList.add(genOrderDetailFromCart(cartInfo));
                cartInfo.setSold("1");
                cartInfo.setUpdateTime(UserSession.getCurDateTime());
            }
            cartInfoService.saveOrUpdateBatch(cartList, 10);
        }

        OrderInfo orderInfo = genOrderInfo(orderDetailList);

        orderInfoService.saveOrUpdate(orderInfo);
        for (OrderDetail orderDetail : orderDetailList) {
            orderDetail.setOrderId(orderInfo.getId());
        }
        orderDetailService.saveOrUpdateBatch(orderDetailList, 10);


        return orderInfo;


    }


    private OrderInfo genOrderInfo(List<OrderDetail> orderDetailList) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderDetailList(orderDetailList);

        String tradeInfo = orderInfo.getTradeInfo();

        orderInfo.setUserId(UserSession.getUserInfo().getId());
        orderInfo.setTradeBody(tradeInfo);
        orderInfo.setCreateTime(UserSession.getCurDateTime());
        orderInfo.setExpireTime(DateUtils.addMinutes(UserSession.getCurDateTime(), 15));
        orderInfo.setOrderStatus(EduConstant.ORDER_STATUS_UNPAID);
        orderInfo.setOutTradeNo(RandomNumString.getRandNumString(1, 9, 15, ""));
        orderInfo.setProvinceId(Long.parseLong(UserSession.getAppCommon().getAr()));


        BigDecimal sumCouponReduce = BigDecimal.ZERO;
        BigDecimal sumOriginAmount = BigDecimal.ZERO;
        BigDecimal sumFinalAmount = BigDecimal.ZERO;
        for (OrderDetail orderDetail : orderDetailList) {
            sumCouponReduce = sumCouponReduce.add(orderDetail.getCouponReduce());
            sumOriginAmount = sumOriginAmount.add(orderDetail.getOriginAmount());
            sumFinalAmount = sumFinalAmount.add(orderDetail.getFinalAmount());
        }
        orderInfo.setOriginAmount(sumOriginAmount);
        orderInfo.setCouponReduce(sumCouponReduce);
        orderInfo.setFinalAmount(sumFinalAmount);
        orderInfo.setSessionId(UserSession.getSessionId());

        return orderInfo;
    }

    private OrderDetail genOrderDetailFromCart(CartInfo cartInfo) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCourseId(cartInfo.getCourseId());
        orderDetail.setCourseName(cartInfo.getCourseName());
        orderDetail.setOriginAmount(cartInfo.getCartPrice());
        orderDetail.setCouponReduce(BigDecimal.ZERO);
        orderDetail.setFinalAmount(orderDetail.getOriginAmount().subtract(orderDetail.getCouponReduce()));
        orderDetail.setUserId(UserSession.getUserInfo().getId());
        orderDetail.setCreateTime(UserSession.getCurDateTime());
        return orderDetail;
    }


    private OrderDetail genOrderDetailFromCourse(CourseInfo courseInfo) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCourseId(courseInfo.getId());
        orderDetail.setCourseName(courseInfo.getCourseName());
        orderDetail.setOriginAmount(courseInfo.getActualPrice());
        orderDetail.setCouponReduce(BigDecimal.ZERO);
        orderDetail.setFinalAmount(orderDetail.getOriginAmount().subtract(orderDetail.getCouponReduce()));
        orderDetail.setUserId(UserSession.getUserInfo().getId());
        orderDetail.setCreateTime(UserSession.getCurDateTime());
        return orderDetail;
    }

}
